package com.cyy.dyeing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyy.dyeing.common.CurrentUser;
import com.cyy.dyeing.common.DataStatusEnum;
import com.cyy.dyeing.dto.LoginDto;
import com.cyy.dyeing.dto.UserDto;
import com.cyy.dyeing.exception.BusinessException;
import com.cyy.dyeing.mapper.UserMapper;
import com.cyy.dyeing.model.Role;
import com.cyy.dyeing.model.User;
import com.cyy.dyeing.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: cyy
 * @Date: 2024/7/22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据登录用户返回当前用户
     * @param loginDto
     * @return
     */
    @Override
    public CurrentUser findUser(LoginDto loginDto){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getU_id, loginDto.getU_id())
                .ne(User::getU_status, 99);
        User user = this.getOne(wrapper);
        CurrentUser currentUser = new CurrentUser(user.getU_id(), user.getU_pwd());
        return currentUser;
    }

    /**
     * 更新用户状态
     * @param u_id
     * @param currentStatus 用户当前状态
     * @return
     */
    @Override
    public String executeChangeStatus(String u_id, String currentStatus) {
        String message = null; //操作提示信息
        String targetStatus = null;// 要更新的目标状态
        if(DataStatusEnum.UNDETERMINED.getCode().equals(currentStatus)){
            targetStatus = DataStatusEnum.DETERMINED.getCode();
            message = "用户信息确认成功";
        }else if(DataStatusEnum.DETERMINED.getCode().equals(currentStatus)){
            targetStatus = DataStatusEnum.ACTIVE.getCode();
            message = "用户启用成功";
        }else if(DataStatusEnum.ACTIVE.getCode().equals(currentStatus)){
            targetStatus = DataStatusEnum.DISABLED.getCode();
            message = "用户禁用成功";
        }else if(DataStatusEnum.DISABLED.getCode().equals(currentStatus)){
            targetStatus = DataStatusEnum.ACTIVE.getCode();
            message = "用户启用成功";
        }
        if(targetStatus == null){
            throw new BusinessException("用户当前状态未知，不能修改");
        }

        //调用mapper实现更改
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(User::getU_status, targetStatus);
        int i = userMapper.updateStatus(u_id, currentStatus, targetStatus);
        System.out.println(currentStatus);
        System.out.println("target: " + targetStatus);
        System.out.println(message);
        if(i > 0){
            return message;
        }

        throw  new BusinessException("更新用户状态失败");
    }

    @Override
    public User getUserById(String uId) {
        return userMapper.getUserById(uId);
    }


}
